package com.GlobalPaint.app.ui;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;

import com.GlobalPaint.app.Base.BaseActivity;
import com.GlobalPaint.app.R;
import com.GlobalPaint.app.bean.ReturnStatusResultEntity;
import com.GlobalPaint.app.bean.ServerTO;
import com.GlobalPaint.app.bean.UpdateTO;
import com.GlobalPaint.app.ui.Guide.GuideActivity;
import com.GlobalPaint.app.utils.CommUtility;
import com.GlobalPaint.app.utils.Constants;
import com.GlobalPaint.app.utils.DataManager;
import com.GlobalPaint.app.utils.DownloadService;
import com.blankj.utilcode.utils.AppUtils;
import com.blankj.utilcode.utils.SPUtils;
import com.google.gson.Gson;

import java.util.ArrayList;

public class SplashActivity extends BaseActivity {
    private Integer recLen = 0;
    private Integer total = 3;
    Handler handler = new Handler();
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            recLen++;
            int remain = total - recLen;
            if (remain == 0) {
                Intent intent = new Intent(SplashActivity.this, GuideActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
                finish();
                return;
            }
            handler.postDelayed(this, 1000);
        }
    };



    private UpdateTO updateTO;
    public static final int CMD_CHECKAPP_UPDATE = 0x0003;
    public static final int CMD_DOWN_APP = 0x0004;
    private Handler mHandler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case CMD_CHECKAPP_UPDATE:
                    SPUtils spUtils = new SPUtils(DataManager.context, "login");
                    String serverUrl = spUtils.getString("serverUrl");
                    if (TextUtils.isEmpty(serverUrl)) {
                        serverUrl = DataManager.baseUrl;
                    }

                    if(spUtils.getString("serverUrl")==null){
                        final ArrayList<ServerTO> arrayList = CommUtility.getServerList();
                        serverUrl = arrayList.get(0).serverUrl;
                        String serverApi = arrayList.get(0).api;
                        spUtils.putString("serverUrl", serverUrl);
                        spUtils.putString("api", serverApi);
                        DataManager.baseUrl=serverUrl;
                    }else {
                        DataManager.baseUrl= spUtils.getString("serverUrl");

                    }


                    String apkVersion="2";         //app=1 人员定位校方  =2 人员定位家长
                    String urlString = DataManager.baseUrl + "/update/1/"+apkVersion;
                    DataManager.getInst().getHttpRequestJsonClass(urlString, Constants.key, UpdateTO.class, new DataManager.Callback() {
                        @Override
                        public void onSuccess(Object o) {
                            updateTO = (UpdateTO) o;

                            if (TextUtils.equals(updateTO.version, String.valueOf(AppUtils.getAppInfo(getApplicationContext()).getVersionName()))) {
                                runnable.run();
                            } else {
                                updateTO.msg = updateTO.msg.replace("@", "\n");
                                if (updateTO.forceUpdate == 1) {
                                    new AlertDialog.Builder(SplashActivity.this)
                                            .setMessage(updateTO.msg)
                                            .setNegativeButton("马上更新", new DialogInterface.OnClickListener() {
                                                @Override
                                                public void onClick(DialogInterface dialog, int which) {
                                                    mHandler.sendEmptyMessage(CMD_DOWN_APP);
                                                    runnable.run();
                                                }
                                            }).show();
                                } else {
                                    AlertDialog.Builder builder = new AlertDialog.Builder(SplashActivity.this);
                                    builder.setMessage(updateTO.msg);
                                    builder.setPositiveButton("马上更新", new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                            dialog.dismiss();
                                            mHandler.sendEmptyMessage(CMD_DOWN_APP);
                                            runnable.run();
                                        }
                                    });
                                    builder.setNegativeButton("以后再说", new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                            dialog.dismiss();
                                            runnable.run();
                                        }
                                    });
                                    //不关闭写法
                                    builder.setCancelable(false);
                                    builder.create().show();
                                }

                            }
                        }
                        @Override
                        public void onFailure(ReturnStatusResultEntity error) {
//                            CommUtility.ShowMsgShort(context, error.msg);
                            runnable.run();
                        }
                    });
                    break;
                case CMD_DOWN_APP:
                    Intent intent = new Intent(getApplicationContext(), DownloadService.class);
                    intent.putExtra("updateinfo", new Gson().toJson(updateTO));
                    startService(intent);
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        CommUtility.verifyStoragePermissions(this);//添加高版本的权限
        runnable.run();
       //mHandler.sendEmptyMessage(CMD_CHECKAPP_UPDATE);
    }
}
